package com.java83.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java83.dto.zilei.OneDto;
import com.java83.entity.RbacManager;
import com.java83.entity.myResponseEntity;
import com.java83.service.RbacManagerService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (RbacManager)表控制层
 *
 * @author makejava
 * @since 2021-11-27 19:37:36
 */
@RestController
@RequestMapping("rbacManager")
public class RbacManagerController {
    /**
     * 服务对象
     */
    @Resource
    private RbacManagerService rbacManagerService;

    /**
     * 分页查询
     *
//     * @param rbacManager 筛选条件
     * @return 查询结果
     */
    @PostMapping("data")
    public myResponseEntity<PageInfo<RbacManager>> queryByPage(@RequestBody OneDto<RbacManager> dto) {
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<RbacManager> managerList = rbacManagerService.queryByPage(dto.getData());
        PageInfo<RbacManager> pageInfo = new PageInfo<>(managerList);
        return new myResponseEntity(200,"chenggong",pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<RbacManager> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.rbacManagerService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param rbacManager 实体
     * @return 新增结果
     */
    @PostMapping
    public myResponseEntity<Integer> add(@RequestBody RbacManager rbacManager) {
        System.out.println(rbacManager);
        int insert = rbacManagerService.insert(rbacManager);
        if(insert == 0){
            return new myResponseEntity<Integer>(500,"服务器炸了",0);
        }else{
            return new myResponseEntity<Integer>(200,"新增成功",insert);
        }
    }

    /**
     * 编辑数据
     *
     * @param rbacManager 实体
     * @return 编辑结果
     */
    @PutMapping
    public myResponseEntity<RbacManager> edit(@RequestBody RbacManager rbacManager) {
        RbacManager update = this.rbacManagerService.update(rbacManager);
        if(update.getAccount() == null){
            return new myResponseEntity<RbacManager>(500,"服务器炸了",null);
        }else{
            return new myResponseEntity<RbacManager>(200,"修改成功",update);
        }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    public myResponseEntity<Integer> deleteById(@PathVariable Integer id) {
        int i = rbacManagerService.deleteById(id);
        if(i > 0){
            return new myResponseEntity<Integer>(200,"删除成功",i);
        }else{
            return new myResponseEntity<Integer>(500,"删除失败，服务器很卡",null);
        }
    }

}

